package com.aaron.kjviewutils;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.aaron.kjviewutils.eventBus.ConEventMsg;
import com.aaron.kjviewutils.eventBus.EventBusMsg;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Retrofit;

/**
 * 作者：Aaron on 2017/2/16 15:13
 * 邮箱：kaijin.fang@edianniu.com
 */
public class FragmentRegister extends Fragment {

    private LoginActivty rootAty;
    private View rootView;

    @Bind(R.id.email)
    EditText email;
    @Bind(R.id.userName)
    EditText userName;
    @Bind(R.id.userPsw)
    EditText userPsw;
    @Bind(R.id.userRePsw)
    EditText userRePsw;
    @Bind(R.id.userPhone)
    EditText userPhone;
    @Bind(R.id.realName)
    EditText realName;
    @Bind(R.id.rigisterBtnL)
    RelativeLayout rigisterBtnL;
    @Bind(R.id.loginL)
    RelativeLayout loginL;

    private Retrofit retrofit;

    @OnClick(R.id.loginL)
    void toLogin()
    {
        rootAty.eventBus.post(new EventBusMsg(EventBusMsg.EventBusType.PRE_LOGIN));
    }

    @OnClick(R.id.rigisterBtnL)
    void register()
    {
        if(TextUtils.isEmpty(email.getText()))
        {
            Toast.makeText(rootAty, "请输入emial", Toast.LENGTH_SHORT).show();
        }
        else if(TextUtils.isEmpty(userName.getText()))
        {
            Toast.makeText(rootAty, "请输入userName", Toast.LENGTH_SHORT).show();
        }
        else if(TextUtils.isEmpty(userPsw.getText()))
        {
            Toast.makeText(rootAty, "请输入密码", Toast.LENGTH_SHORT).show();
        }
        else if(TextUtils.isEmpty(userRePsw.getText()))
        {
            Toast.makeText(rootAty, "请确认密码", Toast.LENGTH_SHORT).show();
        }
        else if(TextUtils.isEmpty(userPhone.getText()))
        {
            Toast.makeText(rootAty, "请输入手机", Toast.LENGTH_SHORT).show();
        }
        else if(TextUtils.isEmpty(realName.getText()))
        {
            Toast.makeText(rootAty, "请输入姓名", Toast.LENGTH_SHORT).show();
        }
        else if(userRePsw.getText().equals(userPsw.getText()))
        {
            Toast.makeText(rootAty, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
        }
        else
        {
            AccountBean account = new AccountBean();
            AccountBean.InnerAccount innerAccount = account.new InnerAccount();
            innerAccount.username = userName.getText().toString();
            innerAccount.password = userPsw.getText().toString();
            innerAccount.email = email.getText().toString();
            account.result = innerAccount;
            if(null!=rootAty.eventBus)
            {
                rootAty.eventBus.postSticky(new ConEventMsg(ConEventMsg.ConEventBusType.DO_REGISTER, account));
            }
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        rootAty = (LoginActivty) getActivity();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.frgment_register, container, false);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        rootAty = null;
    }
}
